package com.founder.devwork.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.founder.devwork.dto.AuthorStatsDTO;
import com.founder.devwork.dto.MonthlyStatDTO;
import com.founder.devwork.dto.ProjectSummaryDTO;
import com.founder.devwork.dto.RepositorySummaryDTO;
import com.founder.devwork.entity.Project;
import com.founder.devwork.entity.ProjectRepository;
import com.founder.devwork.persistence.mapper.ProjectMapper;
import com.founder.devwork.service.ProjectRepositoryService;
import com.founder.devwork.service.ProjectService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 项目服务实现类
 */
@Slf4j
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    private ProjectRepositoryService projectRepositoryService;

    @Override
    public List<RepositorySummaryDTO> getRepositoryByProjectId(String id) {
        return baseMapper.getRepositoryByProjectId(id);
    }

    @Override
    public IPage<ProjectSummaryDTO> getProjectSummaries(IPage<ProjectSummaryDTO> page, String name, String sortBy, String sortDir) {
        return baseMapper.getProjectSummaries(page, name, sortBy, sortDir);
    }

    @Override
    public List<MonthlyStatDTO> getProjectMonthlyStats(String projectId) {
        return baseMapper.getProjectMonthlyStats(projectId);
    }

    @Override
    public List<AuthorStatsDTO> getProjectAuthorStats(String projectId) {
        return baseMapper.getProjectAuthorStats(projectId);
    }

    /*
     * @inheritDoc
     */
    @Override
    public void removeProjWithRelationOfRepoByProjId(String projId) {
        log.info("removeProjWithRelationOfRepoByProjId: {}", projId);
        super.removeById(projId);
        projectRepositoryService.remove(new LambdaQueryWrapper<ProjectRepository>().eq(ProjectRepository::getProjectId, projId));
        log.info("removeProjWithRelationOfRepoByProjId: {} end", projId);
    }
}
